package cn.edu.scau.dormitory_management_system.service.impl;

import cn.edu.scau.dormitory_management_system.constant.FilePath;
import cn.edu.scau.dormitory_management_system.entity.DormbuildEntity;
import cn.edu.scau.dormitory_management_system.entity.StudentEntity;
import cn.edu.scau.dormitory_management_system.service.*;
import cn.edu.scau.dormitory_management_system.util.ExcelUtil;
import cn.edu.scau.dormitory_management_system.util.FileUtil;
import cn.edu.scau.dormitory_management_system.util.PoiUtil;
import cn.edu.scau.dormitory_management_system.util.ZipUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;

/**
 * @author djc
 * @date 2021/8/6
 * @time 9:40
 */

@Service
public class ExportServiceImpl implements ExportService {

    @Autowired
    private BillService billService;

    @Autowired
    private DormService dormService;

    @Autowired
    private DormbuildService dormbuildService;

    @Autowired
    private StudentService studentService;

    private <T> List<T> getExportListByCondition(IService<T> iService,
                                  String key, Object value,
                                  String descCol) {
        QueryWrapper<T> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc(descCol);
        wrapper.eq(key, value);
        return iService.list(wrapper);
    }

    private void downloadSingleFile(int option, HttpServletResponse response) {
        String filePath = FilePath.TEMP_PATH + File.separator
                + ExcelUtil.getFileNameByOption(option) + ".xlsx";
        File file = new File(filePath);
        FileUtil.downloadFile(response, file, file.getName());

        //清空文件
        FileUtil.deleteFile(FilePath.TEMP_PATH);
    }

    private void downloadZipFile(int option, HttpServletResponse response) {
        String filePath = FilePath.ZIP_PATH + File.separator
                + ZipUtil.getZipName(option);
        ZipUtil.zipCompress(FilePath.TEMP_PATH, filePath);

        File file = new File(filePath);
        FileUtil.downloadFile(response, file, file.getName());

        //清空文件
        FileUtil.deleteFile(FilePath.TEMP_PATH);
        FileUtil.deleteFile(FilePath.ZIP_PATH);
    }

    private IService<?> getService(int option) {
        switch (option) {
            case 0:
                return billService;
        }
        return null;
    }

    @Override
    public void creteExcelFile(int option, List<String> value,
                               HttpServletResponse response) {
        if(option == 1) {
            for (String obj : value) {
                int id = Integer.parseInt(obj);
                DormbuildEntity entity = dormbuildService.getById(id);
                ExcelUtil.setNameListByIdx(1, entity.getDormBuildName() + "楼栋学生信息");
                PoiUtil.createExcelFile(1, dormService.getStudentDto(id));
            }
        }else if(option == 2) {
            for (String obj : value) {
                int id = Integer.parseInt(obj);
                DormbuildEntity entity = dormbuildService.getById(id);
                ExcelUtil.setNameListByIdx(2, entity.getDormBuildName() + "楼栋宿舍信息");
                PoiUtil.createExcelFile(2, dormService.findDormByBuildId(id));
            }
        }else if(option == 4) {
            for (String obj : value) {
                ExcelUtil.setNameListByIdx(4, obj + "专业宿舍信息");
                PoiUtil.createExcelFile(4, dormService.findDormByDepartment(obj));
            }
        }else {
            for (String obj : value) {
                ExcelUtil.setNameListByIdx(3, obj + "专业学生信息");
                QueryWrapper<StudentEntity> wrapper = new QueryWrapper<>();
                wrapper.eq("department", obj);
                PoiUtil.createExcelFile(3, studentService.list(wrapper));
            }
        }
        if(value.size() > 1) downloadZipFile(option, response);
        else downloadSingleFile(option, response);
    }

    @Override
    public void createExcelFile(String key, List<?> value,
                                String descCol, int option,
                                HttpServletResponse response) {
        IService<?> service = getService(option);
        assert service != null;
        for (Object obj : value) {
            if("dorm_build_id".equals(key)) {
                ExcelUtil.setNameListByIdx(0,  obj + "楼栋账单");
            }
            if("dorm_name".equals(key)) {
                ExcelUtil.setNameListByIdx(0, obj + "宿舍账单");
            }
            PoiUtil.createExcelFile(option, getExportListByCondition(service,
                    key, obj, descCol));
        }
        if(value.size() > 1) downloadZipFile(option, response);
        else downloadSingleFile(option, response);
    }

}
